#include<bits/stdc++.h>
using namespace std;

int a[101][101];
int main(){
    //最多苹果数与最小苹果数的差
    int m,n;
    cin >> m >> n;
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
        }
    }
    int maxn = 0,minn = 10001;
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            if(a[i][j] > maxn){
                maxn = a[i][j];
            }
            if(a[i][j] < minn){
                minn = a[i][j];
            }
        }
    }
    cout << maxn - minn;


    
    return 0;
}